package com.you.atlas.biz.place.service.impl;

import com.you.atlas.biz.place.service.CommentService;
import com.you.common.ibatis.data.DataList;
import com.you.common.ibatis.param.QueryParam;
import com.you.common.result.ServiceResult;
import com.you.member.biz.service.NotifyService;
import com.you.member.dal.dao.dataobject.NotifyDO;
import com.you.place.dal.dao.AlbumDAO;
import com.you.place.dal.dao.CommentDAO;
import com.you.place.dal.dao.NoteDAO;
import com.you.place.dal.dao.dataobject.AlbumDO;
import com.you.place.dal.dao.dataobject.CommentDO;
import com.you.place.dal.dao.dataobject.NoteDO;

public class CommentServiceImpl implements CommentService {

    private CommentDAO    commentDAO;

    private NoteDAO       noteDAO;

    private AlbumDAO      albumDAO;

    private NotifyService notifyService;

    public void setNoteDAO(NoteDAO noteDAO) {
        this.noteDAO = noteDAO;
    }

    public void setAlbumDAO(AlbumDAO albumDAO) {
        this.albumDAO = albumDAO;
    }

    public void setCommentDAO(CommentDAO commentDAO) {
        this.commentDAO = commentDAO;
    }

    public void setNotifyService(NotifyService notifyService) {
        this.notifyService = notifyService;
    }

    @Override
    public boolean postComment(CommentDO commentDO) {
        String type = commentDO.getType();
        String title =null;
        Long memberId = null;
        if (type.equals("note")) {
            NoteDO noteDO =noteDAO.find(commentDO.getFkId());
            if(noteDO==null){
                return false;
            }
            title=noteDO.getTitle();
            memberId= noteDO.getMemberId();
            noteDAO.addCommentCount(commentDO.getFkId());
        } else if (type.equals("photo")) {
            AlbumDO albumDO = albumDAO.find(commentDO.getFkId());
            if(albumDO==null){
                return false;
            }
            title = albumDO.getTitle();
            memberId= albumDO.getMemberId();
            albumDAO.addCommentCount(commentDO.getFkId());
        }
        boolean result = commentDAO.createComment(commentDO);
        if(!commentDO.getPostMemberId().equals(memberId)){
            NotifyDO notifyDO = new NotifyDO();
            notifyDO.setTitle(title);
            notifyDO.setType(type);
            notifyDO.setReceiveMemberId(memberId);
            notifyDO.setFkId(commentDO.getFkId());
            notifyService.send(notifyDO);
        }
        return result;
    }

    @Override
    public ServiceResult<Object> deleteComment(Long id, Long memberId) {
        CommentDO commentDO = find(id);

        if (commentDO == null) {
            return ServiceResult.newFailureResult(null);
        }

        if (!commentDO.getPostMemberId().equals(memberId)) {
            return ServiceResult.newFailureResult(null).setModule(commentDO);
        }

        String type = commentDO.getType();
        if (type.equals("note")) {
            noteDAO.subtractCommentCount(commentDO.getFkId());
        } else if (type.equals("photo")) {
            albumDAO.subtractCommentCount(commentDO.getFkId());
        }
        commentDAO.delete(id);
        return ServiceResult.newSuccessResult().setModule(commentDO);
    }

    @Override
    public CommentDO find(Long id) {
        return commentDAO.find(id);
    }

    @Override
    public DataList queryByFKId(int pageNo, String type, Long fkId) {
        QueryParam<String, Object> queryParam = new QueryParam<String, Object>();
        queryParam.setPageNo(pageNo);
        queryParam.setPageSize(10);
        queryParam.put("type", type);
        queryParam.put("fkId", fkId);
        return commentDAO.queryByFKId(queryParam);
    }

}
